package com.lcxw.hxzbapi.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.$Gson$Preconditions;
import com.lcxw.hxzbapi.controller.membercontroller.MemberRealization;
import com.lcxw.hxzbapi.entity.pojo.RichLevel;
import com.lcxw.hxzbapi.service.ConfigService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.lcxw.hxzbapi.entity.pojo.Gift;
import com.lcxw.hxzbapi.entity.pojo.YwpdGiftBag;
import com.lcxw.hxzbapi.service.GiftBagService;
import com.lcxw.hxzbapi.service.GiftService;

@Component
public class GiftBagRealization {

    @Resource
    GiftBagService giftBagService;

    @Resource
    MemberRealization memberRealization;

    @Resource
    GiftService giftService;

    @Resource
    ConfigService configService;

    public List<JSONObject> getAllGiftBag(long userId) throws Exception {
        List<YwpdGiftBag> ywpdGiftBags = giftBagService.selectByUserId(userId);

        List<JSONObject> data = new ArrayList<>();

        RichLevel richLevel = memberRealization
            .getRichLevel(memberRealization.getConsumeTotal(userId));

        //判断是否启用游乐广场
        if (configService.getEnableCube() == 1) {
            //判断该用户的富豪等级是否达到了5级，如果达到了，则将游乐广场加入到背包中
            if (richLevel.getLevel() >= 5) {
                Gift play = giftService.selectByPrimaryKey(1);
                JSONObject play_json = new JSONObject();
                play_json.put("giftName", play.getGiftName());
                play_json.put("giftIcon", play.getGiftIcon());
                play_json.put("flash", play.getFlash());
                play_json.put("play", true);
                data.add(play_json);
            }
        }

        for (YwpdGiftBag ywpdGiftBag : ywpdGiftBags
        ) {
            JSONObject giftBag_json = new JSONObject();
            Gift gift = giftService.selectByPrimaryKey(ywpdGiftBag.getGiftId());
            if (gift != null) {
                giftBag_json.put("giftId", ywpdGiftBag.getGiftId());
                giftBag_json.put("giftNum", ywpdGiftBag.getGiftNum());
                giftBag_json.put("userId", ywpdGiftBag.getUserId());
                giftBag_json.put("addTime", ywpdGiftBag.getAddTime());
                giftBag_json.put("expire", ywpdGiftBag.getExpire());
                giftBag_json.put("giftName", gift.getGiftName());
                giftBag_json.put("giftIcon", gift.getGiftIcon());
                giftBag_json.put("flash", gift.getFlash());
            }

            data.add(giftBag_json);
        }
        return data;
    }
}
